package com.zyl.blog.controller.admin;/**
 * @author 14323
 * @date 2020/8/8 15:01
 */

import com.zyl.blog.pojo.Type;
import com.zyl.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * @Description: TODO
 * @author: scott
 * @date: 2020年08月08日 15:01
 */
@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @GetMapping("/types")
    public String types(
            @PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
            Model model){

        model.addAttribute("page",typeService.listType(pageable));
        typeService.listType(pageable);
        return "admin/types";
    }

    @GetMapping("/types/input")
    public String input(Model model){
        model.addAttribute("type", new Type());
        return "admin/types-input";
    }

    @GetMapping("/types/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {
        model.addAttribute("type", typeService.getType(id));
        return "admin/types-input";
    }

    @PostMapping("/types")
    public String post(@Valid Type type, BindingResult result, RedirectAttributes attributes){
        //非重复验证
        Type validType = typeService.getTypeByName(type.getName());
        if (validType != null) {
            result.rejectValue("name", "nameError", "不能添加重复的分类！");
        }
        if (result.hasErrors()) {
            return "admin/types-input";
        }

        Type resType = typeService.saveType(type);
        if (resType == null) {
            attributes.addFlashAttribute("message", "添加失败！");
        } else {
            attributes.addFlashAttribute("message", "添加成功！");
        }
        return "redirect:/admin/types";
    }

    //BindingResult必须和要校验的值放在一起（@Valid Type type, BindingResult result中间不能有其他参数）
    @PostMapping("/types/{id}")
    public String editPost(@Valid Type type, BindingResult result,@PathVariable Long id, RedirectAttributes attributes){
        //非重复验证
        Type validType = typeService.getTypeByName(type.getName());
        if (validType != null) {
            result.rejectValue("name", "nameError", "请输入新分类的名称！");
        }
        if (result.hasErrors()) {
            return "admin/types-input";
        }

        Type resType = typeService.updateType(id, type);
        if (resType == null) {
            attributes.addFlashAttribute("message", "更新失败！");
        } else {
            attributes.addFlashAttribute("message", "更新成功！");
        }
        return "redirect:/admin/types";
    }

    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        typeService.deleteType(id);
        attributes.addFlashAttribute("message", "删除成功！");
        return "redirect:/admin/types";
    }

}
